﻿namespace WMS6.Web.Configuration
{
    using System;
    using System.Collections.Generic;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;

    public class ReceiptValidationRule : BasePage
    {
        protected Button btnCopy;
        protected Button btnDelete;
        protected Button btnGoBack;
        protected Button btnNew;
        protected Button btnSave;
        protected CheckBox chkDefaultFlag;
        protected CheckBox chkSysCreated;
        protected DropDownList ddlCommodityNotOnAsn;
        protected DropDownList ddlCommodityNotOnPo;
        protected DropDownList ddlIsQtyValidation;
        protected DropDownList ddlReceiptWithoutASN;
        protected DropDownList ddlReceiptWithoutLPN;
        protected DropDownList ddlReceiptWithoutPO;
        protected HtmlForm F24051;
        protected HtmlInputHidden hdnFlag;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnReceiptValidationID;
        protected HtmlHead Head1;
        protected Label lblCommodityNotOnAsn;
        protected Label lblCommodityNotOnPo;
        protected Label lblDescr;
        protected Label lblIsQtyValidation;
        protected Label lblMsg;
        protected Label lblOveragePercent;
        protected Label lblReceiptValidationID;
        protected Label lblReceiptValidationRule;
        protected Label lblReceiptWithoutASN;
        protected Label lblReceiptWithoutLPN;
        protected Label lblReceiptWithoutPO;
        protected HtmlLink mycss;
        protected Label one1;
        protected ScriptManager ScriptManager1;
        protected TextBox txtDescr;
        protected TextBox txtOveragePercent;
        protected TextBox txtReceiptValidationID;
        protected UpdatePanel up1;

        protected void btnCopy_Click(object sender, EventArgs e)
        {
            this.txtReceiptValidationID.Text = "";
            this.txtReceiptValidationID.Enabled = true;
            this.hdnReceiptValidationID.Value = "";
            this.chkSysCreated.Checked = false;
            this.lblMsg.Text = "";
            this.lblMsg.Visible = false;
            this.hdnFlag.Value = "0";
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            if (this.chkSysCreated.Checked)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C011");
            }
            else if (!string.IsNullOrEmpty(this.txtReceiptValidationID.Text.Trim()))
            {
                try
                {
                    List<string> areaID = new List<string> {
                        this.txtReceiptValidationID.Text.Trim()
                    };
                    this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Delete(areaID, "RECEIPT_VALIDATION") + base.GetGlobalMsgResource("C006");
                    this.btnSave.Enabled = false;
                    this.btnDelete.Enabled = false;
                    this.btnCopy.Enabled = false;
                    base.DataEntityViewState = null;
                }
                catch (Exception ex)
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
                }
            }
        }

        protected void btnGoBack_Click(object sender, EventArgs e)
        {
            switch (this.hdnFrom.Value)
            {
                case "query":
                    base.Response.Redirect("ReceiptValidationRuleQuery.aspx", false);
                    break;

                case "list":
                    base.Response.Redirect("ReceiptValidationRuleList.aspx?from=edit", false);
                    break;
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.txtReceiptValidationID.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F24050_001");
                this.txtReceiptValidationID.Focus();
            }
            else if (string.IsNullOrEmpty(this.txtDescr.Text.Trim()))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("F24050_002");
                this.txtDescr.Focus();
            }
            else if (this.hdnFlag.Value == "1")
            {
                this.UpdateReceiptValidationInfo();
            }
            else
            {
                this.InsertReceiptValidationInfo();
            }
        }

        private void InsertReceiptValidationInfo()
        {
            try
            {
                ReceiptValidationInfo receiptValidationInfo = new ReceiptValidationInfo {
                    ReceiptValidationID = this.txtReceiptValidationID.Text.Trim(),
                    Descr = this.txtDescr.Text.Trim(),
                    SystemCreated = this.chkSysCreated.Checked ? "Y" : "N",
                    DefaultFlag = this.chkDefaultFlag.Checked ? "Y" : "N"
                };
                if (!string.IsNullOrEmpty(this.txtOveragePercent.Text))
                {
                    receiptValidationInfo.OveragePercent = decimal.Parse(this.txtOveragePercent.Text);
                }
                receiptValidationInfo.IsQtyValidation = this.ddlIsQtyValidation.SelectedValue;
                receiptValidationInfo.ReceiptWithoutAsn = this.ddlReceiptWithoutASN.SelectedValue;
                receiptValidationInfo.ReceiptWithoutLpn = this.ddlReceiptWithoutLPN.SelectedValue;
                receiptValidationInfo.ReceiptWithoutPo = this.ddlReceiptWithoutPO.SelectedValue;
                receiptValidationInfo.CommodityNotOnAsn = this.ddlCommodityNotOnAsn.SelectedValue;
                receiptValidationInfo.CommodityNotOnPo = this.ddlCommodityNotOnPo.SelectedValue;
                receiptValidationInfo.CreatedBy = base.UserInfo.UserID;
                receiptValidationInfo.IsActive = "Y";
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Insert(receiptValidationInfo, "RECEIPT_VALIDATION") + base.GetGlobalMsgResource("C004");
                base.DataEntityViewState = receiptValidationInfo;
                this.hdnFlag.Value = "1";
                this.hdnReceiptValidationID.Value = receiptValidationInfo.ReceiptValidationID;
                this.txtReceiptValidationID.Enabled = false;
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C005") + ex.Message;
            }
        }

        private void LoadData()
        {
            base.BindData2DDLBySysCode(this.ddlCommodityNotOnAsn, "ValidationOption");
            base.BindData2DDLBySysCode(this.ddlCommodityNotOnPo, "ValidationOption");
            base.BindData2DDLBySysCode(this.ddlIsQtyValidation, "WhetherOption");
            base.BindData2DDLBySysCode(this.ddlReceiptWithoutASN, "ValidationOption");
            base.BindData2DDLBySysCode(this.ddlReceiptWithoutLPN, "ValidationOption");
            base.BindData2DDLBySysCode(this.ddlReceiptWithoutPO, "ValidationOption");
            if (!string.IsNullOrEmpty(base.Request.QueryString["ID"]))
            {
                this.hdnReceiptValidationID.Value = base.Request.QueryString["ID"];
                this.SetReceiptValidationInfo2Form();
                this.hdnFlag.Value = "1";
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                this.hdnFrom.Value = base.Request.QueryString["from"];
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetReceiptValidationInfo2Form()
        {
            try
            {
                string ReceiptValidationID = this.hdnReceiptValidationID.Value;
                ReceiptValidationInfo receiptValidationInfo = BaseInfoServiceClientProxy.GetInstance().GetInfoByID(ReceiptValidationID, "RECEIPT_VALIDATION") as ReceiptValidationInfo;
                base.DataEntityViewState = receiptValidationInfo;
                if (receiptValidationInfo != null)
                {
                    this.txtReceiptValidationID.Text = receiptValidationInfo.ReceiptValidationID;
                    this.txtDescr.Text = receiptValidationInfo.Descr;
                    this.chkDefaultFlag.Checked = receiptValidationInfo.DefaultFlag == "Y";
                    this.chkSysCreated.Checked = receiptValidationInfo.SystemCreated == "Y";
                    this.txtReceiptValidationID.Enabled = false;
                    this.txtOveragePercent.Text = base.GetPreferenceDigit(receiptValidationInfo.OveragePercent);
                    this.ddlIsQtyValidation.SelectedValue = receiptValidationInfo.IsQtyValidation;
                    this.ddlReceiptWithoutASN.SelectedValue = receiptValidationInfo.ReceiptWithoutAsn;
                    this.ddlReceiptWithoutLPN.SelectedValue = receiptValidationInfo.ReceiptWithoutLpn;
                    this.ddlReceiptWithoutPO.SelectedValue = receiptValidationInfo.ReceiptWithoutPo;
                    this.ddlCommodityNotOnAsn.SelectedValue = receiptValidationInfo.CommodityNotOnAsn;
                    this.ddlCommodityNotOnPo.SelectedValue = receiptValidationInfo.CommodityNotOnPo;
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void UpdateReceiptValidationInfo()
        {
            try
            {
                ReceiptValidationInfo receiptValidationInfo = base.DataEntityViewState as ReceiptValidationInfo;
                receiptValidationInfo.Descr = this.txtDescr.Text.Trim();
                receiptValidationInfo.DefaultFlag = this.chkDefaultFlag.Checked ? "Y" : "N";
                if (!string.IsNullOrEmpty(this.txtOveragePercent.Text))
                {
                    receiptValidationInfo.OveragePercent = decimal.Parse(this.txtOveragePercent.Text);
                }
                receiptValidationInfo.IsQtyValidation = this.ddlIsQtyValidation.SelectedValue;
                receiptValidationInfo.ReceiptWithoutAsn = this.ddlReceiptWithoutASN.SelectedValue;
                receiptValidationInfo.ReceiptWithoutLpn = this.ddlReceiptWithoutLPN.SelectedValue;
                receiptValidationInfo.ReceiptWithoutPo = this.ddlReceiptWithoutPO.SelectedValue;
                receiptValidationInfo.CommodityNotOnAsn = this.ddlCommodityNotOnAsn.SelectedValue;
                receiptValidationInfo.CommodityNotOnPo = this.ddlCommodityNotOnPo.SelectedValue;
                receiptValidationInfo.UpdatedBy = base.UserInfo.UserID;
                receiptValidationInfo.IsActive = "Y";
                this.lblMsg.Text = BaseInfoServiceClientProxy.GetInstance().Update(receiptValidationInfo, "RECEIPT_VALIDATION") + base.GetGlobalMsgResource("C002");
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C003") + ex.Message;
            }
        }
    }
}

